#! /usr/bin/env python

def scale_data(data_list):
    MAX_SCALE = 1
    MIN_SCALE = 0
    max_data = max(data_list)
    min_data = min(data_list)
    data_scale_list = []
    for data in data_list:
        data_scale = (data - min_data)/(max_data - min_data) * (MAX_SCALE - MIN_SCALE) + MIN_SCALE   
        data_scale_list.append(data_scale)
    return data_scale_list

def scale_file(srcfilename, desfilename):  
    f_src = open(srcfilename, 'r')
    features_list = []
    scale_features_list = []
    scale_epocs_list = []
    for line in f_src:
        features = line.strip().split()
        features_list.append(features)
    f_src.close()

    if features_list:
        feature_length = len(features)
        for feature_index in range(feature_length):
            features_data = [float(features[feature_index]) for features in features_list]
            scale_features = scale_data(features_data)
            scale_features_list.append(scale_features)

        epocs_length = len(scale_features)
        print epocs_length
        for epocs_index in range(epocs_length):
            scale_epocs = [str(data[epocs_index]) for data in scale_features_list]
            scale_epocs_list.append(scale_epocs)

        f_des = open(desfilename, 'w')
        for epocs in scale_epocs_list:
            f_des.write("  ".join(epocs))
            f_des.write('\n')

scale_file('r.txt.20131313', 'scale_r')